package NWish
{
	import NWish.Interface.ILogger;
	
	import away3d.core.base.Object3D;
	
	import flash.events.*;
	import flash.net.FileFilter;
	import flash.net.FileReference;
	import flash.utils.Timer;
	
	public class Misc
	{
		public function Misc()
		{
		}
		
		static public function getChildrenByNameInScene(name,parent,array:Array)
		{
			var num = parent.numChildren;
			for(var i = 0; i < num; ++i)
			{
				var c = parent.getChildAt(i);
				if(c.name && c.name.search(name) != -1)
					array.push(c);
				else if(name == null)
					array.push(c);
				
				getChildrenByNameInScene(name,c,array);
			}
		}
		
		static public function getChildByNameInScene(name,parent,without_obj = null,deep = true)
		{
			var num = parent.numChildren;
			for(var i = 0; i < num; ++i)
			{
				var c = parent.getChildAt(i);
				if(c.name == name && without_obj != c)
					return c;

				if(deep)
				{
					var cc = getChildByNameInScene(name,c,without_obj,deep);
					if(cc)return cc;
				}
			}
			
			return null;
		}
		
		static public function foreachChildren(parent,function_)
		{
			var num = parent.numChildren;
			for(var i = 0; i < num; ++i)
			{
				var c = parent.getChildAt(i);
				function_(c);
				
				foreachChildren(c,function_);
			}
		}
		
		
		static public function pack_list(component_list,x = 0,y = 0, h = 16,separated = 2)
		{
			for each(var c in component_list)
			{
				c.pack();
				
				c.x = x;
				c.height = h;
				c.y = y;
				
				x += c.width + separated;
			}
		}
		
		static public function pack(component,h = 14)
		{
			var num = component.getComponentCount();
			var x = 0;
			for(var i = 0; i < num; ++i)
			{
				var c = component.getComponent(i);
				
				c.pack();
				
				c.x = x;
				c.height = h;
				
				x += c.width + 4;
			}
			
			
			/*component.pack();
			component.width += 32;
			component.height = 15;*/
		}
		
		static public function validName(name:String)
		{
			if(name.search('#') != -1)
			{
				ILogger.log("valid char '#' in string : " + name);
				name = name.replace('#','');
			}
			
			/*if(name.search('@') != -1)
			{
				ILogger.log("valid char '@' in string : " + name);
				name = name.replace('@','');
			}*/
			
			return name;
		}
		
		
		static private const MAX:uint = 2048;
		static public function isBitmapDataValid(bmd)
		{
			var w:int = bmd.width;
			var h:int = bmd.height;
			
			if(w<2 || h<2 || w>MAX || h>MAX)
				return false;
			
			if(isPowerOfTwo(w) && isPowerOfTwo(h))
				return true;
			
			return false;
		}
		
		static public function isPowerOfTwo(value:int): Boolean
		{
			return value ? ((value & -value) == value) : false;
		}
		
		
		static public function getObjectName(o)
		{
			var msg:String = '';
			msg += o;
			if(o is Object3D)
			{
				msg = msg.replace("object ",'');
				msg = msg.replace("]",'  ' + o.name + "]");
			}
			return msg;
		}
		
		static public function isClass(obj,c:Class)
		{
			var cn:String = c + '';
			cn = cn.replace("class ",'');
			cn = cn.replace("[",'');
			cn = cn.replace("]",'');

			
			var obj_str:String = obj;
			obj_str = obj_str.replace("object ",'');
			obj_str = obj_str.replace("[",'');
			obj_str = obj_str.replace("]",'');
			

			return obj_str == cn;
		}
		
		
		static public function ioErrorHandler(e:IOErrorEvent):void
		{
			ILogger.log("io Error : " + e);
		}
		
		static public function securityErrorHandler(e:SecurityErrorEvent):void
		{
			ILogger.log("security Error : " + e);
		}
		
		static public function setObjNoSave(obj):void
		{
			if(obj.extra == null)
				obj.extra = new Object();
			
			if(obj.extra.customProperties == null)
				obj.extra.customProperties = new Object();
			
			obj.extra.customProperties.isSave = false;
		}
		
		static public function setObjSave(obj):void
		{
			if(obj.extra == null)
				return;
			
			if(obj.extra.customProperties == null)
				return;
			
			if(obj.extra.customProperties.isSave == null)
				return;
			
			obj.extra.customProperties.isSave = true;
		}
		
		static public function callLater(delay,fun)
		{
			var tr:Timer = new Timer(delay,1);
			tr.addEventListener(TimerEvent.TIMER,_fun);
			tr.start();
			
			function _fun(e:Event)
			{
				e.target.removeEventListener(TimerEvent.TIMER,_fun);
				
				fun();
			}
		}
	}
}










